package com.lineage.server.clientpackets;

import com.add.L1PcUnlock;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.CharObjidTable;
import com.lineage.server.datatables.ClanMembersTable;
import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.WorldClan;
import java.util.Collection;
import java.util.Iterator;

public class C_CreateClan extends ClientBasePacket {
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport()) {
        String s = readS();
        int i = s.length();
        if (i > 16) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(98));
          return;
        } 
        if (!C_CreateChar.isInvalidName(s)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(53));
          return;
        } 
        if (!pc.isCrown()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(85));
          return;
        } 
        if (pc.getClanid() != 0) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(86));
          return;
        } 
        Collection<L1Clan> allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getClanName().toLowerCase().equals(s.toLowerCase())) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(99));
            return;
          } 
        } 
        if (!pc.getInventory().checkItem(40308, 30000L)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
          return;
        } 
        L1Clan clan2 = ClanReading.get().createClan(pc, s);
        ClanMembersTable.getInstance().newMember(pc);
        if (clan2 != null) {
          pc.getInventory().consumeItem(40308, 30000L);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(84, s));
          L1PcUnlock.Pc_Unlock(pc);
          CharObjidTable.get().addClan(clan2.getClanId(), clan2.getClanName());
          return;
        } 
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
